﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMercadoLibre.Models;
//using Spring.Web.Mvc;
using System.Net;
using System.Text;
using System.IO;

namespace MvcMercadoLibre.Controllers
{
    public class PanelController : Controller
    {
        //
        // GET: /Panel/

        public ActionResult Index()
        {
            ArticulosWS.BuscarArticuloImplService WSarticulos = new ArticulosWS.BuscarArticuloImplService();
            List<Articulo> lista = new List<Articulo>();
            var articulos = WSarticulos.ListarArticulos();
            foreach (ArticulosWS.articulo item in articulos)
            {
                try
                {
                    lista.Add(new Articulo().convierte(item));
                }
                catch (Exception e)
                { }
            }

            return View(lista);
        }

        public ActionResult Aceptar(int id)
        {
            Uri address = new Uri("http://localhost:8080/MercadoLibreRes/services");
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Method = "POST";
            string requestBody = "";
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(requestBody);
            request.ContentLength = byteData.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(byteData, 0, byteData.Length);
            }

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode == HttpStatusCode.Created)
                {
                    string location = response.Headers["Location"];
                    if (location != null)
                    {
                        ViewBag.Message = "OK";
                    }
                }
                else { ViewBag.Mesagge = "KO"; }
            }
            return View();
        }

        public ActionResult Rechazar(int id)
        {
            var lista = Session["listaPendientes"] as List<Articulo>;
            lista.Remove(obtieneArticulo(id));
            return View(lista);
        }

        public void inicializar()
        {
            if (Session["listaPendientes"] == null)
            {
                var arts = new List<Articulo>{  
                new Articulo { Id=1, Nombre= "Samsung Galaxy Ace", Precio=899, Cantidad=2, UrlImagen="1.jpg",Comprador="Nelson Campos" },
                new Articulo { Id=2, Nombre= "Polos 100% Algodon", Precio=60, Cantidad=3, UrlImagen="2.jpg",Comprador="Carlos Rosales" },
                new Articulo { Id=3, Nombre= "Zapatillas Puma Ferrari Blancas", Precio=220, Cantidad=4, UrlImagen="3.jpg", Comprador="Frank Hinostroza" },
                new Articulo { Id=4, Nombre= "Cámara filmadora Genius Digital", Precio=399, Cantidad=2, UrlImagen="4.jpg",Comprador="Edwin Palomares" }};
                Session.Add("listaPendientes", arts);
            }
        }
        private Articulo obtieneArticulo(int id)
        {
            inicializar();
            Articulo articulo = new Articulo();
            foreach (Articulo item in Session["listaPendientes"] as List<Articulo>)
            {
                if (item.Id == id)
                {
                    articulo = item;
                }
            }
            return articulo;
        }
    }
}
